package com.sky;

import com.alibaba.fastjson.JSONObject;
import com.sky.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
@Slf4j
public class HttpClientDemo {
    /**
     * 测试get请求
     */
    @Test
    public void testGet() throws IOException {
        //1.创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2.创建请求对象 httpGet,添加要访问的请求url地址
        HttpGet httpGet=new HttpGet("http://localhost:8080/user/shop/status");

        //3.发送请求execute
        CloseableHttpResponse response = httpClient.execute(httpGet);

        //4.获取响应对象以及解析
        int statusCode = response.getStatusLine().getStatusCode();
        log.info("解析的状态响应码为:{}",statusCode);

        HttpEntity entity = response.getEntity();
        String body = EntityUtils.toString(entity);
        log.info("响应结果的数据为:{}",body);

        //关闭流资源
        response.close();
        httpClient.close();
    }

    /**
     * 测试post请求
     */
    @Test
    public void testPost() throws IOException {
        //1.创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2.创建请求对象 httpPost,添加要访问的请求url地址
        HttpPost httpPost=new HttpPost("http://localhost:8080/admin/employee/login");

        //3.设置请求体
        //3.3 设置请求参数
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("username","admin");
        jsonObject.put("password","123456");
        //3.2 创建请求体对象，设置请求类型和编码格式
        StringEntity stringEntity=new StringEntity(jsonObject.toString());
        stringEntity.setContentEncoding("utf8");
        stringEntity.setContentType("application/json");
        //3.1 设置请求体
        httpPost.setEntity(stringEntity);
        
        

        //4.发送请求execute
        CloseableHttpResponse response = httpClient.execute(httpPost);

        //5.获取响应对象以及解析
        int statusCode = response.getStatusLine().getStatusCode();
        log.info("解析的状态响应码为:{}",statusCode);

        HttpEntity entity = response.getEntity();
        String body = EntityUtils.toString(entity);
        log.info("响应结果的数据为:{}",body);

        //关闭流资源
        response.close();
        httpClient.close();
    }

    /**
     * 测试get请求
     */
    @Test
    public void testGetUtils() throws IOException {
        String url="http://localhost:8080/user/shop/status";
        Map<String, String> paramMap=new HashMap<>();
        String body = HttpClientUtil.doGet(url, paramMap);
        System.out.println(body);
    }

    /**
     * 测试post请求
     */
    @Test
    public void testPostUtils() throws IOException {
        String url="http://localhost:8080/admin/employee/login";

        Map<String, String> paramMap=new HashMap<>();
        paramMap.put("username","admin");
        paramMap.put("password","123456");
        String body = HttpClientUtil.doPost4Json(url, paramMap);
        System.out.println(body);
    }
}
